home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZDICT.C < prev    next >
C/C++ Source or Header  |  1991-12-29  |  6KB  |  256 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zdict.c */
  21. /* Dictionary operators for GhostScript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "dict.h"
  26. #include "store.h"
  27.  
  28. /* dict */
  29. int
  30. zdict(register os_ptr op)
  31. {    check_type(*op, t_integer);
  32.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  33.         return e_rangecheck;
  34.     return dict_create((uint)op->value.intval, op);
  35. }
  36.  
  37. /* maxlength */
  38. int
  39. zmaxlength(register os_ptr op)
  40. {    check_type(*op, t_dictionary);
  41.     check_dict_read(*op);
  42.     make_int(op, dict_maxlength(op));
  43.     return 0;
  44. }
  45.  
  46. /* setmaxlength */
  47. int
  48. zsetmaxlength(register os_ptr op)
  49. {    uint new_size;
  50.     int code;
  51.     os_ptr op1 = op - 1;
  52.     check_type(*op1, t_dictionary);
  53.     check_dict_write(*op1);
  54.     check_type(*op, t_integer);
  55.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  56.         return e_rangecheck;
  57.     new_size = (uint)op->value.intval;
  58.     if ( dict_length(op - 1) > new_size )
  59.         return e_dictfull;
  60.     code = dict_resize(op - 1, new_size);
  61.     if ( code >= 0 ) pop(2);
  62.     return code;
  63. }
  64.  
  65. /* begin */
  66. int
  67. zbegin(register os_ptr op)
  68. {    check_type(*op, t_dictionary);
  69.     check_dict_read(*op);
  70.     if ( dsp == dstop ) return e_dictstackoverflow;
  71.     ++dsp;
  72.     ref_assign(dsp, op);
  73.     pop(1);
  74.     return 0;
  75. }
  76.  
  77. /* end */
  78. int
  79. zend(register os_ptr op)
  80. {    if ( dsp == dstack + 1 ) return e_dictstackunderflow;
  81.     dsp--;
  82.     return 0;
  83. }
  84.  
  85. /* def */
  86. int
  87. zdef(register os_ptr op)
  88. {    int code;
  89.     check_op(2);
  90.     if ( r_has_type(op - 1, t_null) ) return e_typecheck;
  91.     check_dict_write(*dsp);
  92.     code = dict_put(dsp, op - 1, op);
  93.     if ( !code ) pop(2);
  94.     return code;
  95. }
  96.  
  97. /* load */
  98. int
  99. zload(register os_ptr op)
  100. {    ref *pvalue;
  101.     check_op(1);
  102.     if ( r_has_type(op, t_null) ) return e_typecheck;
  103.     if ( r_has_type(op, t_name) )
  104.        {    /* Use the fast lookup */
  105.         if ( (pvalue = dict_find_name(op)) == 0 )
  106.             return e_undefined;
  107.        }
  108.     else
  109.        {    if ( dict_lookup(dstack, dsp, op, &pvalue) <= 0 )
  110.             return e_undefined;
  111.        }
  112.     ref_assign(op, pvalue);
  113.     return 0;
  114. }
  115.  
  116. /* store */
  117. int
  118. zstore(register os_ptr op)
  119. {    ref *pvalue;
  120.     int code;
  121.     check_op(2);
  122.     if ( r_has_type(op - 1, t_null) ) return e_typecheck;
  123.     if ( dict_lookup(dstack, dsp, op - 1, &pvalue) <= 0 )
  124.        {    code = dict_put(dsp, op - 1, op);
  125.         if ( code ) return code;
  126.        }
  127.     else
  128.         ref_assign_old(pvalue, op, "store");
  129.     pop(2);
  130.     return 0;
  131. }
  132.  
  133. /* get - implemented in zgeneric.c */
  134.  
  135. /* put - implemented in zgeneric.c */
  136.  
  137. /* undef */
  138. int
  139. zundef(register os_ptr op)
  140. {    check_type(op[-1], t_dictionary);
  141.     check_dict_write(op[-1]);
  142.     if ( !r_has_type(op, t_null) )
  143.         dict_undef(op - 1, op);    /* ignore undefined error */
  144.     pop(2);
  145.     return 0;
  146. }
  147.  
  148. /* known */
  149. int
  150. zknown(register os_ptr op)
  151. {    os_ptr op1 = op - 1;
  152.     ref *pvalue;
  153.     check_type(*op1, t_dictionary);
  154.     check_dict_read(*op1);
  155.     make_bool(op1,
  156.           (r_has_type(op, t_null) ? 0 :
  157.            dict_find(op1, op, &pvalue) > 0 ? 1 : 0));
  158.     pop(1);
  159.     return 0;
  160. }
  161.  
  162. /* where */
  163. int
  164. zwhere(register os_ptr op)
  165. {    ref *pdref = dsp;
  166.     ref *pvalue;
  167.     check_op(1);
  168.     if ( r_has_type(op, t_null) )
  169.        {    make_bool(op, 0);
  170.         return 0;
  171.        }
  172.     while ( 1 )
  173.        {    check_dict_read(*pdref);
  174.         if ( dict_find(pdref, op, &pvalue) > 0 ) break;
  175.         if ( --pdref < dstack )
  176.            {    make_bool(op, 0);
  177.             return 0;
  178.            }
  179.        }
  180.     ref_assign(op, pdref);
  181.     push(1);
  182.     make_bool(op, 1);
  183.     return 0;
  184. }
  185.  
  186. /* copy for dictionaries -- called from zcopy in zgeneric.c. */
  187. /* Only the type of *op has been checked. */
  188. int
  189. zcopy_dict(register os_ptr op)
  190. {    os_ptr op1 = op - 1;
  191.     check_type(*op1, t_dictionary);
  192.     check_dict_read(*op1);
  193.     check_dict_write(*op);
  194.     if ( dict_length(op) != 0 || dict_maxlength(op) < dict_length(op1) )
  195.         return e_rangecheck;
  196.     dict_copy(op1, op);
  197.     ref_assign(op - 1, op);
  198.     pop(1);
  199.     return 0;
  200. }
  201.  
  202. /* currentdict */
  203. int
  204. zcurrentdict(register os_ptr op)
  205. {    push(1);
  206.     ref_assign(op, dsp);
  207.     return 0;
  208. }
  209.  
  210. /* countdictstack */
  211. int
  212. zcountdictstack(register os_ptr op)
  213. {    push(1);
  214.     make_int(op, dsp - dstack + 1);
  215.     return 0;
  216. }
  217.  
  218. /* dictstack */
  219. int
  220. zdictstack(register os_ptr op)
  221. {    int depth = dsp - dstack + 1;
  222.     check_write_type(*op, t_array);
  223.     if ( depth > r_size(op) ) return e_rangecheck;
  224.     r_set_size(op, depth);
  225.     refcpy_to_old(op->value.refs, dstack, depth, "dictstack");
  226.     return 0;
  227. }
  228.  
  229. /* cleardictstack */
  230. int
  231. zcleardictstack(os_ptr op)
  232. {    dsp = dstack + 1;
  233.     return 0;
  234. }
  235.  
  236. /* ------ Initialization procedure ------ */
  237.  
  238. op_def zdict_op_defs[] = {
  239.     {"0cleardictstack", zcleardictstack},
  240.     {"1begin", zbegin},
  241.     {"0countdictstack", zcountdictstack},
  242.     {"0currentdict", zcurrentdict},
  243.     {"2def", zdef},
  244.     {"1dict", zdict},
  245.     {"0dictstack", zdictstack},
  246.     {"0end", zend},
  247.     {"2known", zknown},
  248.     {"1load", zload},
  249.     {"1maxlength", zmaxlength},
  250.     {"2setmaxlength", zsetmaxlength},
  251.     {"2store", zstore},
  252.     {"2undef", zundef},
  253.     {"1where", zwhere},
  254.     op_def_end(0)
  255. };
  256.